#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <signal.h>
#include <time.h>

#include "core.h"
#include "user_operation.h"
#include "click.h"
#include "file_operation.h"
#include "guess_number.h"

static void codecheck(char *code, char *checkcode);
static void create_account(struct account_data* core_head, char* core_g_name);
static int login(struct account_data* core_head, char* core_g_name);
static void out_ranking(struct account_data* core_head);

/**
 *@brief        打印初始界面以及选择不同的功能
 */
void menu(struct core* core)
{
    if (!core) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    char ret;
    int mode = 0;

	while (mode != 4) {
		system("clear");
    	printf("\n\n\n\n\n\n\n\n\n\t\t --------------------------------------------\n");
    	printf("\t -----------Please select the operation you need-----------\n");
    	printf("\t\t ---------------------------------------------\n");
    	printf("\n");
    	printf("\t\t ------------------1.Register-----------------\n");
    	printf("\n");
    	printf("\t\t ------------------2.Login--------------------\n");
    	printf("\n");
    	printf("\t    ------------------3.Check the rankings------------------\n");
    	printf("\n");
   		printf("\t\t ------------------4.Sign out------------------\n\n\t\t\t\t\t");
   		
   		mode = 0;
   		while (mode < 1 || mode > 4) {
   			mode = judge_number(4);
   			
   			if (mode < 1 || mode > 4) {
   				printf("\n\t\tplease input 1~4:");
			}
		}
    	
		switch (mode) {
        	case 1:
        		create_account(&core->head, core->current_user_name);
				LOG("用户%s注册成功",core->current_user_name);
          		break;
        	case 2:
            	ret = login(&core->head, core->current_user_name);
            	LOG("用户%s完成登录",core->current_user_name);
            	break;
        	case 3:
            	out_ranking(&core->head);
				LOG("用户%s查看排行榜",core->current_user_name);
            	break;
        	case 4:
            	break;
        	default:
            	break;
    	}
    	
    	if (ret == 1) {
    		mode_select(core);
    		ret = 0;
		}
	}
}

/**
 *@brief        创建新用户
 */
void create_account(struct account_data* core_head, char* core_g_name)
{
    if (!core_head && !core_g_name) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    char name[6];
    char code[6];
    char checkcode[6];
    
    struct account_data* newlist;
    struct account_data *scan_list_1, *scan_list_2;
    
    system("clear");
    printf("\n\n\n\n\n\n\n\n\n\t\t-----------Register-----------\n");
    printf("\t\t请输入字符数小于5的账号名，\n\t\t以及字符数小于6的密码\n");
    printf("\t\tPlease enter your username:");
    scanf("%s", name);
	
    system("stty -echo");
    printf("\t\tPlease enter your code:");
    scanf("%s", code) ;
    printf("\n\t\tPlease enter your code again:");
    scanf("%s", checkcode);
    system("stty echo");

    /*检测两次输入的密码是否相同*/
    codecheck(code, checkcode);

    newlist = (struct account_data *)malloc(sizeof(struct account_data));
    strcpy(newlist->code, code);
    strcpy(newlist->name, name);
    strcpy(core_g_name, name);
    
    newlist->grace = 0;
	
    /*将新节点接入链表中*/    
    scan_list_1 = core_head->next;
    scan_list_2 = core_head->next;
    if (core_head->next == NULL) { 
	    core_head->next = newlist;
	    newlist->next = NULL;
    }
	else {
		while (scan_list_2 != NULL) {
	    	if (scan_list_1->next == NULL) {
            	scan_list_1->next = newlist;
	    		newlist->next = NULL;
	    		scan_list_2 = newlist->next;
	    	}
			else {
				scan_list_1 = scan_list_1->next;
	    		scan_list_2 = scan_list_1;
	     	}
        }
    }
	
    printf("\033[;32m\n\n\tCongratulations!The account application is successful!\n\033[0m");
    sleep(1);
}

/**
 *@brief        核对用户名和密码并登录
 ×return        成功返回1，失败返回0
 */
int login(struct account_data* core_head, char* core_g_name)
{
    if (!core_head && !core_g_name) {
        DEBUG("PARAM ERROR!\n");
        return 0;
    }
	
    char name[6];
    char code[6];
    struct account_data *scan_list;
    
    system("clear");
    printf("\n\n\n\n\n\n\n\n\n\t\t----------Login---------\n");
    printf("\t\tPlease enter your username:");
    scanf("%s", name);
    system("stty -echo");
    printf("\n\t\tPlease enter your code:");
    scanf("%s", code);
    system("stty echo");
    
    /*扫描链表匹配相同的用户名，然后比对密码，失败则返回0,成功返回1*/
    scan_list = core_head->next;
    if (core_head->next == NULL) {
        printf("\033m[;31m\n\t\tLogin failed, please log in again\n\033[0m"); 
        sleep(1);
		system("clear");

	return 0; 
    }
    while (scan_list != NULL) {
        if (strcmp(scan_list->name, name) == 0) { 
            if (strcmp(scan_list->code, code) == 0) {
                printf("\033[;32m\n\t\tLogin success\n\033[0m");
                strcpy(core_g_name, scan_list->name);
				sleep(1);

	        	return 1;
            }
			else {
                printf("\033[;31m\n\t\tThe password is wrong,and you have two chances\n\n\033[0m");
	        	printf("\t\tPlease enter the password again:"); 
				system("stty -echo");
				scanf("%s",code);
	        	system("stty echo");

				if (strcmp(scan_list->code,code) == 0) { 
		    			printf("\033[;32m\n\t\tLogin success\n\033[0m");
            	    	strcpy(core_g_name, scan_list->name);
		    			sleep(1);
					
                   	 	return 1;
				}else { 
		    		printf("\033[;31m\n\t\tThe password is wrong,and you have one chances\n\n\033[0m");
		    		printf("\t\tPlease enter the password again:"); 
		    		system("stty -echo");
		    		scanf("%s",code);
		    		system("stty echo");
		
                	if (strcmp(scan_list->code, code) == 0) { 
                    	printf("\033[;31m\n\t\tLogin success\n\033[0m");
                    	strcpy(core_g_name, scan_list->name);
						sleep(1);
						
						return 1;
		    		}
					else { 
						printf("\033[;32m\n\t\tLogin failed, please log in again\n\033[0m"); 
		        		sleep(1);
						system("clear");
						return 0; 
		    		}
				}
	    	}	
		}
		else{ 
	    	scan_list = scan_list->next;
	    
            if (scan_list == NULL) { 
                printf("\033[;32m\n\t\tLogin failed, please log in again\n\033[0m");
		        sleep(1);
		        system("clear"); 
				
                return 0; 
	    	}
        }
    }
	return 0;
}

/*
 *@brief        free整个链表
 */
void destroy(struct core* core)
{
    if (!core) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    struct account_data *scan_list;
    
    /*先将数据保存到文件后再free*/
    write_to_file(&core->head);
    
    /*删除每一个节点*/
    scan_list = core->head.next;
    while (scan_list != NULL) {
		free(scan_list);
		scan_list = scan_list->next;
    }
    
    free(core);
    LOG("用户%s退出系统",core->current_user_name);
}

/*
 *@brief        打印排行榜
 */
void out_ranking(struct account_data* core_head)
{
    if (!core_head) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    struct account_data *scan_list;
    int rank = 1;
	
    system("clear");
    printf("\n\n\n\n\n\n\n\n\n\t\t---------rank--------");
    scan_list = core_head->next;
    if (core_head->next == NULL) {
		printf("\033[;32m\n\t\tSorry,no data yet\n\033[0m");
        sleep(1);
        return;
    }
	else {
		while (scan_list != NULL){
	    	printf("\n\t\tThe NO.%d name is %s,his achievement is:%d\n",
			rank, scan_list->name, scan_list->grace);
	    	scan_list=scan_list->next;
	    	rank++;
            
        }
        sleep(2);
        return;
    }
}

/*
 *@brief        选择游玩的游戏
 */
void mode_select(struct core* core)
{
    if (!core) {
        DEBUG("PARAM ERROR!\n");
        return;
    }
    
    int mode;
	
    while (mode != 3) {
    	system("clear");
   		printf("\n\n\n\n\n\n\n\n\n\t\t------------game park-------------");
    	printf("\n\t\tWelcome to the game park!\n");
    	printf("\t\tPlease choose the game you want to play:\n");
    	printf("\t\t1-->guess number\n");
    	printf("\t\t2-->crazy click\n");
    	printf("\t\t3-->menu\n");
    	printf("\n");
    	printf("\n\t\t");
    
    	mode = 0;
   		while (mode < 1 || mode > 3) {
   			mode = judge_number(3);
   			
   			if (mode < 1 || mode > 3) {
   				printf("\n\t\tplease input 1~3:");
			}
		}
    	
    	switch (mode) {
	    	case 1:
	    		guess_number(&core->guess_data);
		    	LOG("用户%s游玩猜数字游戏",core->current_user_name);
		    	break;
	    	case 2:
		   		system("clear");
		    	crazy_click(&core->head, core->current_user_name);
		    	break;
	    	case 3:
		    	system("clear");
		    	break;
			default :
				break;
    	}
	}
}

/*
 *@brief        核对密码
 */
void codecheck(char *code,char *checkcode)
{
    if (strcmp(code, checkcode) != 0) {
        printf("\033[;32m\n\t\tThe two input password is different, please retype\n\n\033[0m");
		
        printf("\t\tPlease enter your password:");
		system("stty -echo");
		scanf("%s", code) ;
		printf("\n\t\tPlease enter your password again:");
		scanf("%s", checkcode);
		system("stty echo");
	    
    	if(strcmp (code, checkcode) != 0) { 
        	printf("\033[;32m\n\t\tThe two input password is different, please retype\n\n\033[0m");
		
			printf("\t\tPlease enter your password:");
			system("stty -echo");
			scanf("%s", code);
			printf("\n\t\tPlease enter your password again:");
			scanf("%s", checkcode);
			system("stty echo");

			if (strcmp(code, checkcode) != 0) {
        		printf("\033[;32m\n\t\tThree input errors, please reregister\033[0m");
			}
    	}
    }
}

